package com.heima.test16;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.UUID;

public class FileUploadServer {
    public static void main(String[] args) throws IOException {
        //1 创建服务器的socket对象
        ServerSocket ss = new ServerSocket(20000);
        //2 接收连接
        Socket accept = ss.accept();

        UUID uuid = UUID.randomUUID();
        String fileName = uuid.toString().replace("-", "");

        //创建文件输出流 用来保存客户端传来的文件
        FileOutputStream fos = new FileOutputStream("pics/" + fileName);

        //3 获取输入流 用来接收图片数据
        InputStream is = accept.getInputStream();
        byte[] bytes = new byte[1024];
        int len;
        while ((len = is.read(bytes)) != -1) {
            //保存客户端传来的文件
            fos.write(bytes, 0, len);
        }

        is.close();
        fos.close();
        accept.close();
        ss.close();
    }
}
